home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / plane / _seg_tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  22.0 KB  |  947 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _seg_tree.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. // ------------------------------------------------------------------
  17. //
  18. // full dynamic Segment Trees
  19. //
  20. // Michael Wenzel     (1990)
  21. //
  22. // Implementation as described in
  23. // Kurt Mehlhorn: Data Structures and Algorithms 3
  24. //
  25. // ------------------------------------------------------------------
  26.  
  27.  
  28. #include <LEDA/impl/seg_tree.h>
  29.  
  30. enum left_or_right {left = 0, right = 1};
  31.  
  32. #undef TEST
  33. #undef DUMP
  34.  
  35. #ifdef TEST
  36. #define DPRINT(x) cout << x
  37. #else
  38. #define DPRINT(x)
  39. #endif
  40.  
  41. #ifdef DUMP
  42. #define DDUMP(x) cout << x
  43. #else
  44. #define DDUMP(x)
  45. #endif
  46.  
  47. ostream& operator<<(ostream& s, h_segment& h) 
  48.   s<<"("<<(int)h._x0<<","<<(int)h._x1<<";"<<(int)h._y<<")";
  49.   return s;
  50. }
  51.  
  52. //-------------------------------------------------------------------
  53. // member functions
  54. //-------------------------------------------------------------------
  55.  
  56. int seg_node_tree::cmp(GenPtr x,GenPtr y) const
  57. { return father->seg_cmp((h_segment_p)x,(h_segment_p)y); }
  58.  
  59. // ------------------------------------------------------------
  60. // query
  61. // returns a List of all items it with y0 <= y(it) <= y1
  62.  
  63. list<seg_tree_item> seg_node_tree::query(GenPtr y0, GenPtr y1)
  64.   DPRINT(" query in nodelist\n");
  65.  
  66.   list<seg_tree_item> L;
  67.  
  68.  
  69.   h_segment_p r = father->new_y_h_segment(y0);
  70.   seg_tree_item it = locate(r);
  71.  
  72.   if (it)
  73.   { while ( it!=min() && father->cmp_dim2(key(it)->_y,y0)==0) it = pred(it);
  74.     while ( it && father->cmp_dim2(key(it)->_y,y1)<=0)
  75.     { L.append(it);
  76.       it = succ(it);
  77.      }
  78.    }
  79.  
  80.   delete r;
  81.   return L;
  82. }
  83.  
  84.  
  85. //-------------------------------------------------------------------
  86. // all_items
  87. // returns all items in the nodelist
  88.  
  89. list<seg_tree_item> seg_node_tree::all_items()
  90. {
  91.   list<seg_tree_item> L;
  92.   seg_tree_item z;
  93.  
  94.   forall_seg_tree_items(z,*this)
  95.     L.append(z);
  96.   
  97.   return L;
  98. }
  99.  
  100.  
  101. //-------------------------------------------------------------------
  102.  
  103. Segment_Tree::Segment_Tree() : r(this)  {} 
  104. Segment_Tree::~Segment_Tree() { clear(root); }
  105.  
  106. int Segment_Tree::seg_cmp(h_segment_p p, h_segment_p q)
  107. { int a;
  108.   if (a = cmp_dim2(p->y(), q->y())) return a;
  109.   if (a = cmp_dim1(p->x0(),q->x0())) return a;
  110.   return cmp_dim1(p->x1(), q->x1());
  111. }
  112.  
  113. // ------------------------------------------------------------
  114. // empty
  115. // returns true, iff for all seg_tree_items it in nodelist(it):
  116. //                 key(it) is not a start- or endoordinate of key(it)
  117.  
  118. int Segment_Tree::empty(bb_item it)
  119. {
  120.   DPRINT(" empty of "<<(int)key(it)<<"\n");
  121.   int erg=true;
  122.   seg_tree_item i;
  123.  
  124.   forall_seg_tree_items(i,*info(it))
  125.     if ( (cmp(key(it),x0(i))==0) || (cmp(key(it),x1(i))==0) )
  126.       erg = false;
  127.  
  128.   DPRINT("empty ergibt "<<erg<<"\n");
  129.   return erg;
  130. }   
  131.  
  132. // ------------------------------------------------------------
  133. // lrot() , rrot() , ldrot() , rdrot()
  134. // Rotationen am Knoten p
  135.  
  136. void Segment_Tree::lrot(bb_item p, bb_item q)
  137. {
  138.   bb_item h = p->sohn[right];
  139.  
  140.   DDUMP("lrot "<< (int)key(p)) ; 
  141.     if (q) DDUMP(" Vater " << (int)key(q));
  142.   DDUMP("\n");
  143.  
  144.   p->sohn[right] = h->sohn[left];
  145.   h->sohn[left] = p;
  146.   if (!q)
  147.     root=h;
  148.   else
  149.   { if (cmp(key(h),key(q))>0)
  150.       q->sohn[right]=h;
  151.     else
  152.       q->sohn[left]=h;
  153.   }
  154.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  155.   h->gr=p->groesse()+h->sohn[right]->groesse();
  156.  
  157.                         // update nodelists
  158.  seg_tree_item i;
  159.  
  160.  bb_item re=p->sohn[right];
  161.  bb_item s=p->sohn[left];
  162.  
  163.  seg_node_list help = info(h);
  164.  info(h) = info(p);
  165.  
  166.  info(p) = new seg_node_tree(this);
  167.  
  168.  forall_seg_tree_items(i,*(info(re)))
  169.    if ( ( (!re->blatt()) 
  170.       ||((!start_coord(re,i))&&(!end_coord(re,i))) )
  171.       && ( (!s->blatt())
  172.        ||((!start_coord(s,i))&&(!end_coord(s,i))) ) 
  173.       && (info(s)->member(r.key(i))) )
  174.  
  175.      info(p)->insert(r.key(i));
  176.  
  177.  forall_seg_tree_items(i,*(info(p)))
  178.  { info(re)->del(r.key(i));
  179.    info(s)->del(r.key(i));
  180.  }
  181.  
  182.  forall_seg_tree_items(i,*help)
  183.  { info(re)->insert(r.key(i));
  184.    info(h->sohn[right])->insert(r.key(i));
  185.  } 
  186.  
  187.  delete help;
  188.  
  189. }
  190.  
  191. void Segment_Tree::rrot(bb_item p, bb_item q)
  192. {
  193.   bb_item h = p->sohn[left];
  194.  
  195.   DDUMP("rrot "<< (int)key(p)) ; 
  196.     if (q) DDUMP(" Vater " << (int)key(q));
  197.   DDUMP("\n");
  198.  
  199.   p->sohn[left] = h->sohn[right];
  200.   h->sohn[right] = p;
  201.   if (!q) root=h;
  202.   else
  203.   { if (cmp(key(h),key(q))>0)
  204.       q->sohn[right] = h;
  205.     else
  206.       q->sohn[left] = h; }
  207.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  208.   h->gr=p->groesse()+h->sohn[left]->groesse();
  209.  
  210.                         // update nodelists
  211.  seg_tree_item i;
  212.  
  213.  bb_item re=p->sohn[right];
  214.  bb_item s=p->sohn[left];
  215.  
  216.  seg_node_list help = info(h);
  217.  info(h) = info(p);
  218.  
  219.  info(p) = new seg_node_tree(this);
  220.  /* forall_seg_tree_items(i,*(info(s)))
  221.     if (info(re)->member(r.key(i)))
  222.       info(p)->insert(r.key(i));    */
  223.  
  224.  forall_seg_tree_items(i,*(info(s)))
  225.    if ( ( (!s->blatt()) 
  226.       ||((!start_coord(s,i))&&(!end_coord(s,i))) )
  227.       && ( (!re->blatt())
  228.        ||((!start_coord(re,i))&&(!end_coord(re,i))) ) 
  229.       && (info(re)->member(r.key(i))) )
  230.  
  231.      info(p)->insert(r.key(i));
  232.  
  233.  forall_seg_tree_items(i,*(info(p)))
  234.  { info(re)->del(r.key(i));
  235.    info(s)->del(r.key(i));
  236.  }
  237.  
  238.  forall_seg_tree_items(i,*help)
  239.  { info(s)->insert(r.key(i));
  240.    info(h->sohn[left])->insert(r.key(i));
  241.  } 
  242.  
  243.  delete help;
  244.  
  245. }
  246.  
  247. void Segment_Tree::ldrot(bb_item p, bb_item q)
  248. {
  249.   bb_item h = p->sohn[right];
  250.   bb_item g = h->sohn[left];
  251.  
  252.   DDUMP("ldrot "<< (int)key(p)) ; 
  253.     if (q) DDUMP(" Vater " << (int)key(q));
  254.   DDUMP("\n");
  255.  
  256.   rrot(h,p);
  257.   lrot(p,q);
  258.  
  259.   /*
  260.   p->sohn[right] = g->sohn[left];
  261.   h->sohn[left] =  g->sohn[right];
  262.   g->sohn[left] = p;
  263.   g->sohn[right] = h;
  264.   if (!q) root=g;
  265.   else
  266.   { if (cmp(key(h),key(q))>0)
  267.       q->sohn[right] =g ;
  268.     else 
  269.       q->sohn[left] = g ; }
  270.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  271.   h->gr=h->sohn[left]->groesse()+h->sohn[right]->groesse();
  272.   g->gr=p->groesse()+h->groesse();
  273.                         // update nodelists
  274.   seg_tree_item i;
  275.  
  276.   seg_node_list help = info(g);
  277.   info(g) = info(p);
  278.   info(p) = new seg_node_tree(this);
  279.  
  280.   forall_seg_tree_items(i,*help)
  281.     info(p->sohn[right])->insert(r.key(i));
  282.  
  283.   forall_seg_tree_items(i,*(info(p->sohn[left])))
  284.     if (info(p->sohn[right])->member(r.key(i)))
  285.       info(p)->insert(r.key(i));
  286.  
  287.   forall_seg_tree_items(i,*(info(p)))
  288.   { info(p->sohn[left])->del(r.key(i));
  289.     info(p->sohn[right])->del(r.key(i));
  290.   }
  291.   
  292.   forall_seg_tree_items(i,*(info(h)))
  293.     info(p->sohn[right])->insert(r.key(i));
  294.  
  295.   forall_seg_tree_items(i,*(info(h->sohn[left])))
  296.     if (info(h->sohn[right])->member(r.key(i)))
  297.       info(h)->insert(r.key(i));
  298.  
  299.   seg_node_list help1 = info(h->sohn[right]);
  300.   info(h->sohn[right]) = new seg_node_tree(this);
  301.  
  302.   forall_seg_tree_items(i,*help1)
  303.   { if (!((info(h->sohn[left]))->member(r.key(i))))
  304.       info(h->sohn[right])->insert(r.key(i));
  305.     else
  306.       info(h->sohn[left])->del(r.key(i));
  307.   }
  308.  
  309.   forall_seg_tree_items(i,*help)
  310.     info(h->sohn[left])->insert(r.key(i));
  311.  
  312.   delete help;
  313.   delete help1;
  314. */
  315.    
  316. }
  317.  
  318. void Segment_Tree::rdrot(bb_item p, bb_item q)
  319.  
  320. {
  321.   bb_item h = p->sohn[left];
  322.   bb_item g = h->sohn[right];
  323.  
  324.   DDUMP("rdrot "<< (int)key(p)) ; 
  325.     if (q) DDUMP(" Vater " << (int)key(q));
  326.   DDUMP("\n");
  327.  
  328.   lrot(h,p);
  329.   rrot(p,q);
  330.  
  331. /*  p->sohn[left] = g->sohn[right];
  332.   h->sohn[right] =  g->sohn[left];
  333.   g->sohn[right] = p;
  334.   g->sohn[left] = h;
  335.   if (!q) root=g;
  336.   else
  337.   { if (cmp(key(h),key(q))>0)
  338.       q->sohn[right] =g ;
  339.     else 
  340.       q->sohn[left] = g ; }
  341.   p->gr=p->sohn[left]->groesse()+p->sohn[right]->groesse();
  342.   h->gr=h->sohn[left]->groesse()+h->sohn[right]->groesse();
  343.   g->gr=p->groesse()+h->groesse();
  344.                         // update nodelists
  345.   seg_tree_item i;
  346.  
  347.   seg_node_list help = info(g);
  348.   info(g) = info(p);
  349.   info(p) = new seg_node_tree(this);
  350.  
  351.   forall_seg_tree_items(i,*help)
  352.     info(p->sohn[left])->insert(r.key(i));
  353.  
  354.   forall_seg_tree_items(i,*(info(p->sohn[right])))
  355.     if (info(p->sohn[left])->member(r.key(i)))
  356.       info(p)->insert(r.key(i));
  357.  
  358.   forall_seg_tree_items(i,*(info(p)))
  359.   { info(p->sohn[left])->del(r.key(i));
  360.     info(p->sohn[right])->del(r.key(i));
  361.   }
  362.   
  363.   forall_seg_tree_items(i,*(info(h)))
  364.     info(p->sohn[left])->insert(r.key(i));
  365.  
  366.   forall_seg_tree_items(i,*(info(h->sohn[right])))
  367.     if (info(h->sohn[left])->member(r.key(i)))
  368.       info(h)->insert(r.key(i));
  369.  
  370.   seg_node_list help1 = info(h->sohn[left]);
  371.   info(h->sohn[left]) = new seg_node_tree(this);
  372.  
  373.   forall_seg_tree_items(i,*help1)
  374.   { if (!((info(h->sohn[right]))->member(r.key(i))))
  375.       info(h->sohn[left])->insert(r.key(i));
  376.     else
  377.       info(h->sohn[right])->del(r.key(i));
  378.   }
  379.  
  380.   forall_seg_tree_items(i,*help)
  381.     info(h->sohn[right])->insert(r.key(i));
  382.  
  383.   delete help;
  384.   delete help1;
  385. */
  386.    
  387. }
  388.  
  389.  
  390. //-------------------------------------------------------------------
  391. // insert
  392. // insert a segment into a Segment_tree:             
  393. // - insert x-coordinates in main tree (bb-alpha) 
  394. //          and rotate (if necessary)
  395. // - insert segment on nodes 
  396. //          immediately below the paths to x-coordinates
  397. // - insert segment into the tree of all segments
  398.  
  399. seg_tree_item Segment_Tree::insert(GenPtr x0, GenPtr x1, GenPtr y, GenPtr inf)
  400.  
  401. {
  402.   DPRINT(" insert segment " << (int)x0 << " " << (int)x1 << " " << (int)y << " in main tree \n" );
  403.  
  404.   seg_tree_item inserted,j;
  405.  
  406.   if (!(cmp(x0,x1)))                         // empty segment
  407.     return 0;
  408.  
  409.   copy_dim1(x0);
  410.   copy_dim1(x1);
  411.   copy_dim2(y);
  412.   copy_info(inf);
  413.  
  414.   h_segment_p i = new h_segment(x0,x1,y,inf);
  415.  
  416.   if (inserted=r.lookup(i)) 
  417.   { delete i;
  418.     r.info(inserted)=inf;
  419.     return inserted;
  420.   }
  421.  
  422.   bb_item t,father,p;
  423.   bb_item start,end;
  424.   seg_node_list h;
  425.  
  426.                 // insert start_coordinate
  427.  
  428.   if (!(start=bb_tree::lookup(x0)))        // new coordinate
  429.   { h=new seg_node_tree(this);
  430.     start = sinsert(x0,h);
  431.     t=start;
  432.   
  433.     if (!st.empty())
  434.     { father = st.pop();         // pop father of leaf and set nodelist
  435.       h=new seg_node_tree(this);
  436.       info(father) = h;
  437.       p = succ(t); 
  438.       if (p)
  439.       { list<seg_tree_item> L;
  440.         L=info(p)->all_items();
  441.         forall(j,L)
  442.       { DDUMP("Item "<<*(r.key(j))<<" in Knoten "<<(int)key(p)<<"\n");
  443.           if (end_coord(p,j))
  444.             info(t)->insert(r.key(j));
  445.           else if (!start_coord(p,j))
  446.                { info(father)->insert(r.key(j));
  447.                  info(p)->del(r.key(j));
  448.                }
  449.         }
  450.       }
  451.     }
  452.     
  453.                                  // rebalancieren
  454.     while (!st.empty())
  455.     { t=st.pop();
  456.       father = st.empty() ? 0 : st.top();
  457.       t->gr++;  
  458.       float i = t->bal();
  459.  
  460.       DDUMP("rebal cur=" << (int)key(t) << " groesse= " << t->groesse() << " bal= " << i << " in main tree\n");
  461.  
  462.       if (i < alpha)
  463.         if (t->sohn[right]->bal()<=d) lrot(t,father);
  464.         else ldrot(t,father);
  465.       else if (i>1-alpha) 
  466.              if (t->sohn[left]->bal() > d ) rrot(t,father);
  467.              else rdrot(t,father);
  468.     }
  469.   }              // start coordinate inserted
  470.  
  471.  
  472.  
  473.                 // insert end_coordinate
  474.  
  475.   if (!(end=bb_tree::lookup(x1)))   // new coordinate
  476.   { h=new seg_node_tree(this);
  477.     end = sinsert(x1,h);
  478.     t=end;
  479.  
  480.     if (!st.empty())
  481.     { father = st.pop();         // pop father of leaf and set nodelist
  482.       h=new seg_node_tree(this);
  483.       info(father) = h;
  484.       p = succ(t); 
  485.       if (p)
  486.       { list<seg_tree_item> L;
  487.         L=info(p)->all_items();
  488.         forall(j,L)
  489.         if (end_coord(p,j))
  490.             info(t)->insert(r.key(j));
  491.           else if (!start_coord(p,j))
  492.                { info(father)->insert(r.key(j));
  493.                  info(p)->del(r.key(j));
  494.                }
  495.       }
  496.     }
  497.  
  498.                                // rebalancieren
  499.     while (!st.empty())
  500.     { t=st.pop();
  501.       father = st.empty() ? 0 : st.top();
  502.       t->gr++;  
  503.       float i = t->bal();
  504.  
  505.       DDUMP("rebal cur=" << (int)key(t) << " groesse= " << t->groesse() << " bal= " << i << " in main tree\n");
  506.  
  507.       if (i < alpha)
  508.         if (t->sohn[right]->bal()<=d) lrot(t,father);
  509.         else ldrot(t,father);
  510.       else if (i>1-alpha) 
  511.              if (t->sohn[left]->bal() > d ) rrot(t,father);
  512.              else rdrot(t,father);
  513.     }
  514.   }              // end coordinate inserted
  515.  
  516.  
  517.      // insert segment into nodelists of leaves of coordinates
  518.  
  519.   info(start)->insert(i);  
  520.   info(end)->insert(i);
  521.  
  522.   p=t=root;
  523.   GenPtr x2,x3;
  524.  
  525.                          // same path
  526.   while (p==t)  // start and end coordinate assigned to different leaves
  527.   { x2=key(p);
  528.     DDUMP(" same path " << (int)x0 << " " << (int)x1 << " " << (int)x2 << "\n");
  529.     if ( cmp(x0,x2)<=0 ) p=p->sohn[left];
  530.       else p=p->sohn[right];
  531.     if ( cmp(x1,x2)<=0 ) t=t->sohn[left];
  532.       else t=t->sohn[right];
  533.   }
  534.                      // now paths to lower and upper part
  535.   while (!p->blatt())                // follow lower path
  536.   { 
  537.     x2=key(p);
  538.     DDUMP(" lower path " << (int)x0 << " " << (int)x2 << "\n");
  539.     if ( cmp(x0,x2)>0 ) 
  540.       p=p->sohn[right];
  541.     else
  542.     { info(p->sohn[right])->insert(i);   // insertion into nodelist
  543.       p=p->sohn[left];
  544.     } 
  545.   }
  546.  
  547.   while (!t->blatt())               // follow upper path
  548.   { 
  549.     x3=key(t);
  550.     DDUMP(" upper path " << (int)x1 << " " << (int)x3 << "\n");
  551.     if ( cmp(x1,x3)<=0 ) 
  552.       t=t->sohn[left];
  553.     else
  554.     { info(t->sohn[left])->insert(i);  // insertion into nodelist
  555.       t=t->sohn[right];
  556.     } 
  557.   }
  558.  
  559. #ifdef DUMP
  560.   print_tree();
  561. #endif
  562.  
  563.   return (r.insert(i)); 
  564. }
  565.  
  566.  
  567. //-------------------------------------------------------------------
  568. // delete
  569. // delete a segment in a Segment_tree:
  570. // - delete segment out of the tree of all segments
  571. // - delete segment on nodes 
  572. //          immediately below the paths to x-coordinates
  573. // - delete x-coordinates in main tree (bb-alpha) 
  574. //          and rotate (if necessary)
  575.  
  576. void Segment_Tree::del(GenPtr x0, GenPtr x1, GenPtr y)
  577.  
  578. {
  579.   DPRINT(" delete segment " << (int)x0 << " " << (int)x1 << " " << (int)y << " in main tree\n");
  580.  
  581.   if (!(cmp(x0,x1)))                         // empty segment
  582.     return ;
  583.  
  584.   bb_item p,q,s,t,father;
  585.   seg_tree_item z;
  586.   seg_node_list help;
  587.   h_segment_p deleted;
  588.   h_segment_p i = new h_segment(x0,x1,y);
  589.  
  590.   if (!(t=r.lookup(i)))          // segment not in tree
  591.   { delete i;
  592.     DDUMP("delete: segment not in tree\n");
  593.     return;
  594.   }
  595.  
  596.   deleted = r.key(t);
  597.   r.del(i);                      // delete in tree of all segments
  598.  
  599.   s=t=root;
  600.   GenPtr x2,x3;
  601.  
  602.                  // delete segment in nodelists
  603.  
  604.   while ((s==t)&&(!s->blatt()))       // same path
  605.   { x2=key(s);
  606.     x3=key(t);
  607.     DDUMP(" same path " << (int)x2 << " groesse " << s->groesse() << "\n");
  608.     if ( cmp(x0,x2)<=0 ) s=s->sohn[left];
  609.       else s=s->sohn[right];
  610.     if ( cmp(x1,x3)<=0 ) t=t->sohn[left];
  611.       else t=t->sohn[right];
  612.   }
  613.                      // now paths to lower and upper part
  614.   while (!s->blatt())                // follow lower path
  615.   { 
  616.     x2=key(s);
  617.     DDUMP(" lower path " << (int)x2 << " groesse " << s->groesse() << "\n");
  618.     if ( cmp(x0,x2)>0 ) 
  619.       s=s->sohn[right];
  620.     else
  621.     { info(s->sohn[right])->del(i);   // delete out of nodelist
  622.       s=s->sohn[left];
  623.     } 
  624.   }
  625.   info(s)->del(i);
  626.  
  627.   while (!t->blatt())               // follow upper path
  628.   { 
  629.     x3=key(t);
  630.     DDUMP(" upper path " << (int)x3 << " groesse " << t->groesse() << "\n");
  631.     if ( cmp(x1,x3)<=0 ) 
  632.       t=t->sohn[left];
  633.     else
  634.     { info(t->sohn[left])->del(i);   // delete out of nodelist
  635.       t=t->sohn[right];
  636.     } 
  637.   }
  638.   info(t)->del(i);
  639.                     // delete in main tree if necessary
  640.  
  641.   if(empty(s))                      // delete item of start coordinate
  642.   {
  643.     sdel(x0);
  644.     if (!st.empty())                // father exists 
  645.     { q = st.pop();                 // pop father of leaf and set nodelist
  646.  
  647.       if (!st.empty())
  648.       { p = st.top();
  649.     if (cmp(key(q),key(p))<=0)  // left son deleted
  650.           p = p->sohn[left];
  651.         else                        // right son deleted
  652.       p = p->sohn[right];
  653.       } 
  654.       else                          // root deleted 
  655.     p = root;
  656.  
  657.                                           // set nodelist
  658.       help = info(p);
  659.       info(p) = info(q);
  660.  
  661.       if (p->blatt())
  662.     forall_seg_tree_items(z,*help)
  663.       if ((start_coord(p,z))||(end_coord(p,z)))
  664.         info(p)->insert(r.key(z));
  665.  
  666.       delete help;
  667.       delete q;
  668.     }
  669.  
  670.     delete info(s);
  671.     delete s;
  672.                                // rebalancieren
  673.     while (!st.empty())
  674.     { p=st.pop();
  675.       father = st.empty() ? 0 : st.top();
  676.       p->gr--;  
  677.       float i = p->bal();
  678.  
  679.       DDUMP("rebal cur=" << (int)key(p) << " groesse= " << p->groesse() << " bal= " << i << " in main tree \n");
  680.  
  681.       if (i < alpha)
  682.         if (p->sohn[right]->bal()<=d) lrot(p,father);
  683.         else ldrot(p,father);
  684.       else if (i>1-alpha) 
  685.              if (p->sohn[left]->bal() > d ) rrot(p,father);
  686.          else rdrot(p,father);
  687.     }
  688.   }
  689.  
  690.   if(empty(t))                      // delete item of end coordinate
  691.   {
  692.     sdel(x1);
  693.     if (!st.empty())                // father exists 
  694.     { q = st.pop();                 // pop father of leaf and set nodelist
  695.  
  696.       if (!st.empty())
  697.       { p = st.top();
  698.     if (cmp(key(q),key(p))<=0)  // left son deleted
  699.           p = p->sohn[left];
  700.         else                        // right son deleted
  701.       p = p->sohn[right];
  702.       } 
  703.       else                          // root deleted 
  704.     p = root;
  705.                                           // set nodelist
  706.       help = info(p);
  707.       info(p) = info(q);
  708.  
  709.       if (p->blatt())
  710.     forall_seg_tree_items(z,*help)
  711.       if ((start_coord(p,z))||(end_coord(p,z)))
  712.         info(p)->insert(r.key(z));
  713.  
  714.       delete help;
  715.       delete q;
  716.     }
  717.  
  718.     delete info(t);
  719.     delete t;
  720.  
  721.                                // rebalancieren
  722.     while (!st.empty())
  723.     { p=st.pop();
  724.       father = st.empty() ? 0 : st.top();
  725.       p->gr--;  
  726.       float i = p->bal();
  727.  
  728.       DDUMP("rebal cur=" << (int)key(p) << " groesse= " << p->groesse() << " bal= " << i << " in main tree \n");
  729.  
  730.       if (i < alpha)
  731.         if (p->sohn[right]->bal()<=d) lrot(p,father);
  732.         else ldrot(p,father);
  733.       else if (i>1-alpha) 
  734.              if (p->sohn[left]->bal() > d ) rrot(p,father);
  735.          else rdrot(p,father);
  736.     }
  737.   }
  738.  
  739. #ifdef DUMP
  740.   print_tree();
  741. #endif
  742.  
  743.  
  744.  
  745.   clear_dim1(x0);
  746.   clear_dim1(x1);
  747.   clear_dim2(y);
  748.   clear_info(inf(deleted));
  749.  
  750.   delete i;
  751.   delete deleted;
  752. }
  753.  
  754. //-------------------------------------------------------------------
  755. // query
  756. // returns all items it in the Segment_tree with
  757. //     x0(it) <= x <= x1(it) and y0 <= y(it) <= y1
  758. //    
  759. // by:
  760. // - look for x in the main tree   
  761. //   
  762. // - for all nodes on the path perform a query(y0,y1) on nodelists
  763.  
  764. list<seg_tree_item> Segment_Tree::query(GenPtr x, GenPtr y0, GenPtr y1)
  765.  
  766.   DPRINT("query in main tree " << (int)x << " " << (int)y0 << " " << (int)y1 << "\n");
  767.  
  768.   bb_item it;
  769.   seg_tree_item z;
  770.   list<seg_tree_item> L,l;
  771.  
  772.   search(x);
  773.   if (st.empty()) return L;
  774.  
  775.   if (cmp(x,key(st.top()))==0)             // x-coordinate in tree
  776.     while (!st.empty())
  777.     { it = st.pop();
  778.       l = info(it)->query(y0,y1);
  779.       L.conc(l);
  780.     }
  781.  
  782.   else                                     // x-coordinate not in tree
  783.   {
  784.      if ((cmp(x,key(bb_tree::min()))<0) || (cmp(x,key(bb_tree::max()))>0))
  785.        return L;
  786.    
  787.      list<seg_tree_item> L1;
  788.      while (!st.empty())
  789.      { it = st.pop();
  790.        L1 = info(it)->query(y0,y1);
  791.        forall(z,L1)
  792.      if ((cmp(x,x0(z))>=0) && (cmp(x,x1(z))<=0))
  793.        L.append(z);
  794.        L1.clear();
  795.      }
  796.   }
  797.  
  798.   return L;
  799. }
  800.  
  801. //-------------------------------------------------------------------
  802. // x_infinity_query
  803. // returns a List of all items it with y0 <= y0(it) <= y1
  804.  
  805. list<seg_tree_item> Segment_Tree::x_infinity_query(GenPtr y0, GenPtr y1)
  806. {
  807.   return r.query(y0,y1);
  808. }
  809.  
  810. //-------------------------------------------------------------------
  811. // y_infinity_query
  812. // returns a List of all items it with x0(it) <= x <= x1(it)
  813.  
  814. list<seg_tree_item> Segment_Tree::y_infinity_query(GenPtr x)
  815. {
  816.   bb_item it;
  817.   seg_tree_item z;
  818.   list<seg_tree_item> L;
  819.  
  820.   search(x);
  821.   if (st.empty()) return L;
  822.  
  823.   if (cmp(x,key(st.top()))==0)             // x-coordinate in tree
  824.     while (!st.empty())
  825.     { it = st.pop();
  826.       forall_seg_tree_items(z,*(info(it)))
  827.       L.append(z);
  828.     }
  829.  
  830.   else                                     // x-coordinate not in tree
  831.   {
  832.      if ((cmp(x,key(bb_tree::min()))<0) || (cmp(x,key(bb_tree::max()))>0))
  833.        return L;
  834.    
  835.      list<seg_tree_item> L1;
  836.      while (!st.empty())
  837.      { it = st.pop();
  838.        forall_seg_tree_items(z,*(info(it)))
  839.      if ((cmp(x,x0(z))>=0) && (cmp(x,x1(z))<=0))
  840.        L.append(z);
  841.      }
  842.   }
  843.  
  844.   return L;
  845. }
  846.  
  847.  
  848. //-------------------------------------------------------------------
  849. // all_items
  850. // returns all items in the tree
  851.  
  852. list<seg_tree_item> Segment_Tree::all_items()
  853. {
  854.   return r.all_items();
  855. }
  856.  
  857.  
  858. //-------------------------------------------------------------------
  859. // lookup
  860. // returns a seg_tree_item it with key(it) = (x0,x1,y) if there is one
  861.  
  862. seg_tree_item Segment_Tree::lookup(GenPtr x0, GenPtr x1, GenPtr y)
  863.   DPRINT(" lookup in main tree " << (int)x0 << " " << (int)x1 << " " << (int)y << "\n");
  864.   h_segment_p z = new h_segment(x0,x1,y);
  865.   seg_tree_item i = r.lookup(z);   
  866.   delete z;
  867.   return i;
  868. }
  869.  
  870.  
  871. //-------------------------------------------------------------------
  872. // clear_tree
  873. // delete all Items and Tree of Items
  874.  
  875. void Segment_Tree::clear_tree()
  876. {
  877.   if (!root) return;
  878.  
  879.   clear(root);
  880.  
  881.   seg_tree_item z;
  882.   h_segment_p q;
  883.  
  884.   forall_seg_tree_items(z,r)
  885.   { 
  886.     q=r.key(z);
  887.     clear_dim1(x0(q));
  888.     clear_dim1(x1(q));
  889.     clear_dim2(y(q));
  890.     clear_info(inf(q));
  891.     delete q;
  892.   }
  893.   r.clear();
  894.  
  895.   first = iterator = 0;
  896.   anzahl = 0;
  897. }
  898.  
  899. //-------------------------------------------------------------------
  900. // clear
  901. // delete nodelists and nodes
  902.  
  903.  
  904. void Segment_Tree::clear(bb_item& it)
  905. {
  906.   if (it == 0) return;
  907.   
  908.   if(!it->blatt())
  909.   { clear(it->sohn[left]);
  910.     clear(it->sohn[right]);
  911.   }
  912.  
  913.   delete info(it);
  914.   delete it;
  915.   it = 0;
  916.  
  917. }
  918.  
  919. //-------------------------------------------------------------------
  920. // print     
  921. // prints the tree with nodelists
  922.  
  923. void Segment_Tree::print(bb_item it,string s)
  924. {
  925.   if (!it) return;
  926.  
  927.   seg_tree_item i;
  928.  
  929.   if (!it->blatt())
  930.     print(it->sohn[left],s+"     ");
  931.  
  932.   cout<< s << (int)key(it) <<"\n";
  933.   cout<< s ; 
  934.   forall_seg_tree_items(i,*info(it))
  935.     cout << "[" << (int)r.key(i) << "]:" << *(r.key(i)) << " ";
  936.   newline;
  937.  
  938.   if (!it->blatt())
  939.     print(it->sohn[right],s+"     ");
  940. }
  941.  
  942.